package com.css.dzj.zfjg.modules.tipinfo.service.impl;


import com.css.dzj.zfjg.common.util.ServiceUtil;
import com.css.dzj.zfjg.modules.tipinfo.entity.JgLhjgTipInfoEntity;
import com.css.dzj.zfjg.modules.tipinfo.entity.QJgLhjgTipInfoEntity;
import com.css.dzj.zfjg.modules.tipinfo.mapper.TipInfoMapper;
import com.css.dzj.zfjg.modules.tipinfo.service.JgLhjgTipInfoService;
import com.querydsl.core.QueryResults;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

/**
 * 企业举报信息serviceImpl
 */
@Service
public class JgLhjgTipInfoServiceImpl implements JgLhjgTipInfoService {

    @Autowired
    private TipInfoMapper tipInfoMapper;

    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    @Autowired
    private ServiceUtil serviceUtil;


    /**
     * 添加企业举报信息
     * @param jgLhjgTipInfoEntity
     */
    @Transactional
    @Override
    public void add(JgLhjgTipInfoEntity jgLhjgTipInfoEntity, HttpServletRequest request) {
        try {
            tipInfoMapper.save(jgLhjgTipInfoEntity);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改企业举报信息
     * @param jgLhjgTipInfoEntity
     */
    @Transactional
    @Override
    public void update(JgLhjgTipInfoEntity jgLhjgTipInfoEntity) {

    }

    /**
     * 通过id查找企业举报信息
     * @param id
     * @return
     */
    @Override
    public JgLhjgTipInfoEntity findById(String id) {
        return tipInfoMapper.findById(id).get();
    }


    /**
     * 分页查询所有企业举报信息
     * @return
     */
    @Override
    public QueryResults<JgLhjgTipInfoEntity> queryPage(Pageable pageable) {
        QJgLhjgTipInfoEntity qJgLhjgTipInfoEntity = QJgLhjgTipInfoEntity.jgLhjgTipInfoEntity;
        QueryResults<JgLhjgTipInfoEntity> tipInfoQueryResults = jpaQueryFactory.selectFrom(qJgLhjgTipInfoEntity)
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
        return tipInfoQueryResults;
    }

    /**
     * 物理删除企业举报信息
     * @param jgLhjgTipInfoEntity
     */
    @Override
    public void delete(JgLhjgTipInfoEntity jgLhjgTipInfoEntity) {
        try {
            tipInfoMapper.delete(jgLhjgTipInfoEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    /**
     * 根据任务编码获取企业举报信息
     * @param taskNo
     */
    @Override
    public JgLhjgTipInfoEntity findByTaskNo(String taskNo) {
        QJgLhjgTipInfoEntity q = QJgLhjgTipInfoEntity.jgLhjgTipInfoEntity;
        return jpaQueryFactory.selectFrom(q).where(q.taskNo.eq(taskNo)).fetchOne();
    }

}
